package com.ewzj.bigdata.mvp.presenter;

import android.app.Application;

import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseJson;
import com.ewzj.bigdata.mvp.model.entity.resbean.CollectBean;
import com.ewzj.bigdata.mvp.ui.adapter.CollectAdapter;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.functions.Consumer;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.CollectContract;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;


@ActivityScope
public class CollectPresenter extends BasePresenter<CollectContract.Model, CollectContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private int page=1;
    @Inject
    public CollectPresenter(CollectContract.Model model, CollectContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
    public void loadinitdata(){
        page=1;
        mModel.loadCollectData(page).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<CollectBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<CollectBean> collectBeans) {
                        page++;
                        mRootView.adapterSwipeRefreh(collectBeans);
                    }
                });
    }
    public void swiperefresh(){
        page=1;
        mModel.loadCollectData(page).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<CollectBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<CollectBean> collectBeans) {
                        page++;
                        mRootView.adapterSwipeRefreh(collectBeans);
                    }
                });
    }

    public void loadmoreData(){
        mModel.loadCollectData(page).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new Consumer<List<CollectBean>>() {
                    @Override
                    public void accept(List<CollectBean> collectBeans) throws Exception {
                        mRootView.obtainAdapter().loadMoreComplete();
                        mRootView.adapterInsert(collectBeans);
                        if (collectBeans.size()< AppConstants.PAGE_NUM){
                            mRootView.obtainAdapter().loadMoreEnd();
                        }
                    }
                },throwable -> {
                    mRootView.obtainAdapter().loadMoreFail();
                });
    }

    public void delCollectBean(CollectBean collectBean){
        mModel.delCollectData(collectBean.getId()+"").compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData<CollectBean>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData<CollectBean> collectBeanBaseData) {
                        ArmsUtils.snackbarText("删除成功");
//                        mRootView.obtainAdapter().getData().remove(collectBean);
//                        mRootView.obtainAdapter().notifyDataSetChanged();
                    }
                });
    }
    public void editCollectBean(CollectBean collectBean){

    }
    public void addCollectBean(String title){
        mModel.addCollectData(title).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<CollectBean>(mErrorHandler) {
                    @Override
                    public void onNext(CollectBean collectBean) {
                        mRootView.adapterInsert(collectBean);
                    }
                });
    }
}
